package com.example.baselibs.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.widget.Toast;

import androidx.core.content.FileProvider;

import java.io.File;


public class ActivityUtils {
    public static Toast toast;

    public static void showToast(Context context,String msg){
        if (toast==null){
            toast=Toast.makeText(context,msg,Toast.LENGTH_SHORT);
        }
        toast.setText(msg);
        toast.show();
    }

    /*安装apk*/
    public static void installApk(Context context, String path) {

        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        File file = new File(path);
        Uri apkUri = null;
        if (Build.VERSION.SDK_INT >= 24) {
            apkUri = FileProvider.getUriForFile(context, "com.example.wxb.fileprovider", file);

            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        } else {
            apkUri = Uri.fromFile(file);
        }
        intent.setDataAndType(apkUri,
                "application/vnd.android.package-archive");
        context.startActivity(intent);
    }

    /**
     * 获取当前版本号
     * @return
     */
    public static String getVersion(Context context){
        return getPackageInfo(context).versionName;
    }

    /**
     * 获取当前版本
     * @return
     */
    public static int getVersionCode(Context context){
        return getPackageInfo(context).versionCode;
    }

    public static PackageInfo getPackageInfo(Context context){
        PackageInfo pi=null;
        try {
            PackageManager pm = context.getPackageManager();
            pi = pm.getPackageInfo(context.getPackageName(),
                    PackageManager.GET_CONFIGURATIONS);

            return pi;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pi;
    }

    /**
     * 获取application中指定的meta-data
     * @return 如果没有获取成功(没有对应值，或者异常)，则返回值为空
     */
    public static String getAppMetaData(Context ctx) {
        if (ctx == null || TextUtils.isEmpty("UMENG_CHANNEL")) {
            return null;
        }
        String resultData = null;
        try {
            PackageManager packageManager = ctx.getPackageManager();
            if (packageManager != null) {
                //注意此处为ApplicationInfo，因为友盟设置的meta-data是在application标签中
                ApplicationInfo applicationInfo = packageManager.getApplicationInfo(ctx.getPackageName(), PackageManager.GET_META_DATA);
                if (applicationInfo != null) {
                    if (applicationInfo.metaData != null) {
                        //key要与manifest中的配置文件标识一致
                        resultData = applicationInfo.metaData.getString("UMENG_CHANNEL");
                    }
                }
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return resultData;
    }


    /**
     * 获取屏幕宽度
     * @return
     */
    public static int getScreenWidth(Context context) {
        Activity activity = (Activity) context;
        if (activity == null) return 0;

        DisplayMetrics metrics = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
        return metrics.widthPixels;
    }

    /**
     * 获取屏幕高度
     * @param context
     * @return
     */
    public int getScreenHeight(Context context) {
        Activity activity = (Activity) context;
        if (activity == null) return 0;

        DisplayMetrics metrics = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
        return metrics.heightPixels;
    }

}
